package club.controller;

import club.pojo.Answer;
import club.pojo.User;
import club.service.AnswerService;
import club.util.Message;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/answer")
public class AnswerController {

    @Resource
    private AnswerService answerService;

    /**
     * 创建回复
     *
     * @param session   HttpSession对象，用于获取当前用户信息
     * @param commentId 评论ID，指定回复的评论
     * @param content   回复的内容
     * @return Message对象，返回成功或失败的消息
     */
    @RequestMapping("/create")
    @ResponseBody
    public Message create(HttpSession session, Integer commentId, String content){
        User user = (User) session.getAttribute("user");
        Integer result = answerService.create(user.getId(), commentId, content);
        if (result > 0){
            return Message.success();
        } else {
            return Message.fail();
        }
    }

    /**
     * 根据ID查找回复
     *
     * @param id 回复ID
     * @return Message对象，包含查找到的回复信息
     */
    @RequestMapping("/findById")
    @ResponseBody
    public Message findById(Integer id){
        Answer answer = answerService.findById(id);
        return Message.success().add("answer", answer);
    }

    /**
     * 创建回复的回复
     *
     * @param session   HttpSession对象，用于获取当前用户信息
     * @param replayId  被回复的回复ID
     * @param content   回复的内容
     * @param commentId 评论ID，指定回复的评论
     * @return Message对象，返回成功或失败的消息
     */
    @RequestMapping("/creates")
    @ResponseBody
    public Message creates(HttpSession session, Integer replayId, String content, Integer commentId){
        User user = (User) session.getAttribute("user");
        Integer result = answerService.creates(user.getId(), replayId, content, commentId);
        if (result > 0){
            return Message.success();
        } else {
            return Message.fail();
        }
    }
}